using System;
using System.Data;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A user alert.
	/// </summary>
	public class Alert : DatabaseAware, IDeletable
	{
		internal Alert(Server server, IDataReader reader) : base(server)
		{
      folderName = reader["eFolderName"].ToString();
      folderId = reader["eFolderID"].ToString();
      message = reader["eAlertMessage"].ToString();
      subject = reader["eSubject"].ToString();
      priority = Convert.ToInt32(reader["ePriority"]);
      mapName = reader["eMapName"].ToString();
      stageName = reader["eStageName"].ToString();
      engineName = reader["eEngineName"].ToString();
      deadline = (DateTime)reader["eDeadline"];
      if (reader["eUpdated"] != System.DBNull.Value)
        updated = (DateTime)reader["eUpdated"];
      else 
        updated = Constants.NullDateTime;
      userName = reader["eUserName"].ToString();
      category = reader["eCategory"].ToString();
      if (reader["eAlertNr"] != System.DBNull.Value)
        alertNumber = Convert.ToInt32(reader["eAlertNr"]);
		}

    internal Alert(Server server, Folder folder, string userName) : base(server)
    {
      this.userName = userName;

      folderName = folder.Name;
      folderId = folder.FolderId;
      message = folder.AlertMessage;
      subject = folder.Subject;
      priority = folder.Priority;
      mapName = folder.MapName;
      stageName = folder.StageName;
      deadline = folder.Deadline;
      updated = folder.Updated;
      category = folder.Category;
      alertNumber = 1;
    }

    private string userName;
    /// <summary>
    /// Gets the name of the user that this alert is for.
    /// </summary>
    public string UserName
    {
      get
      {
        return userName;
      }
    }

    private string folderName;
    /// <summary>
    /// Gets the name of the folder.
    /// </summary>
    public string FolderName
    {
      get
      {
        return folderName;
      }
    }

    private string folderId;
    /// <summary>
    /// Gets the folder ID.
    /// </summary>
    public string FolderId
    {
      get
      {
        return folderId;
      }
    }

    private Folder folder;
    /// <summary>
    /// Gets the folder for this alert.
    /// </summary>
    public Folder GetFolder()
    {
      if (folder == null)
        folder = Server.GetFolder(folderId);
      return folder;
    }

    private string message;
    /// <summary>
    /// Gets the alert message.
    /// </summary>
    public string Message
    {
      get
      {
        return message;
      }
    }

    private string subject;
    /// <summary>
    /// Gets the folder subject.
    /// </summary>
    public string Subject
    {
      get
      {
        return subject;
      }
    }

    private int priority;
    /// <summary>
    /// Gets the priority of the folder.
    /// </summary>
    /// <value>The priority.</value>
    public int Priority
    {
      get
      {
        return priority;
      }
    }

    private string mapName;
    /// <summary>
    /// Gets the name of the map for the folder.
    /// </summary>
    public string MapName
    {
      get
      {
        return mapName;
      }
    }

    private string stageName;
    /// <summary>
    /// Gets the name of the stage for the folder.
    /// </summary>
    public string StageName
    {
      get
      {
        return stageName;
      }
    }

    private string engineName;
    /// <summary>
    /// Gets the name of the engine.
    /// </summary>
    public string EngineName
    {
      get
      {
        return engineName;
      }
    }

    private DateTime deadline;
    /// <summary>
    /// Gets the deadline for the folder.
    /// </summary>
    public DateTime Deadline
    {
      get
      {
        return deadline;
      }
    }

    private DateTime updated;
    /// <summary>
    /// Gets the updated time for the folder.
    /// </summary>
    public DateTime Updated
    {
      get
      {
        return updated;
      }
    }

    private string category;
    /// <summary>
    /// Gets the category of the folder.
    /// </summary>
    public string Category
    {
      get
      {
        return category;
      }
    }

    private int alertNumber;
    /// <summary>
    /// Gets the alert number.
    /// </summary>
    public int AlertNumber
    {
      get
      {
        return alertNumber;
      }
    }

    #region IDeletable Members

    /// <summary>
    /// Deletes this alert from the database.
    /// </summary>
    public void Delete()
    {
      Server.ExecuteNonQuery(string.Format("DELETE FROM eAlert WHERE eUserName = '{0}' AND eFolderID = '{1}'", 
        SqlEscape(userName), folderId));
    }

    #endregion

    /// <summary>
    /// Returns the user name for the alert.
    /// </summary>
    public override string ToString()
    {
      return userName;
    }

  }
}
